<?php
namespace app\know\model;

use think\Model;
use think\facade\Log;

class KnowBaseModel extends Model
{
	// 20220927143901-14
	// know_chapter
	// ks2022092716054910000000000
	public function getUUID($length = 32, $ap = 0) 
	{
		$prefix = implode('', array_map(function($s){
			return substr($s, 0, 1);
		}, explode('_', $this->name)));
		
		$uuid = $this->max('uuid', false);
		Log::debug(__CLASS__ . __FUNCTION__ . '-max-' .$uuid);
		$d = date('YmdHis');
		$l = $length - 14 - strlen($prefix);
		if (empty($uuid))
		{
			if ($l <= 2)
			{
				throw new Exception("uuid长度不足");
			}
			$sn = strval(1 + $ap);
		}
		else
		{
			$d0 = substr($uuid, strlen($prefix), 14);
			if ($d == $d0)
			{
				$sn = strval(intval(substr($uuid, $l * -1)) + 1 + $ap);
			}
			else
			{
				$sn = strval(1 + $ap);
			}
		}
		$uuid = $prefix . $d . str_pad($sn, $l, "0", STR_PAD_LEFT);
		Log::debug(__CLASS__ . __FUNCTION__ . $uuid);
		return $uuid;
	}

	public function getTableName()
	{
		return $this->name;
	}
	
	protected function log($d)
	{
		Log::debug($d);
	}

	protected function htmlDecode($c)
    {
        return htmlspecialchars_decode(htmlspecialchars_decode($c));
    }
}